﻿#include "CInit.h"
#include <Windows.h>
#include <d3d9.h>

CInit::CInit(HWND hWnd, HINSTANCE hInstance, LPCSTR Name, int Width, int Height, bool IsFullScreen)
{
	wndHandle      = hWnd;
	hInstance	   = hInstance;
	m_Name		   = Name;
	m_ScreenWidth  = Width;
	m_ScreenHeight = Height;
	m_IsFullScreen = IsFullScreen;

	_pd3d = NULL;
	_pd3dDevice = NULL;
}

CInit::~CInit()
{
	if(_pd3dDevice!=NULL)
	{
		_pd3dDevice->Release();
		_pd3dDevice=NULL;
	}
	if(_pd3d!=NULL)
	{
		_pd3d->Release();
		_pd3d=NULL;
	}
}

/**************************************
* hàm callback
***************************************/
LRESULT CALLBACK CInit::_WinProc(HWND hWnd, UINT message,
						 WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_DESTROY: 
			PostQuitMessage(0);
			break;
		default: 
			return DefWindowProc(hWnd, message, wParam,lParam);
	}

	return 0;
}

/**************************************
* đăng kí của sổ window
***************************************/
bool CInit::initWindow(int nShowCmd)
{
	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style = CS_HREDRAW | CS_VREDRAW; 
	wcex.lpfnWndProc = (WNDPROC) CInit::_WinProc; 
	wcex.cbClsExtra = 0; 
	wcex.cbWndExtra = 0;
	wcex.hIcon = NULL;
	wcex.hInstance = hInstance;
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = m_Name;
	wcex.hIconSm = NULL;

	RegisterClassEx(&wcex);

	DWORD style; 
	if (m_IsFullScreen)
		style = WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP;
	else 
		style = WS_OVERLAPPEDWINDOW;

	wndHandle = CreateWindow(m_Name, 
							 m_Name, 
							 style,
							 CW_USEDEFAULT, 
							 CW_USEDEFAULT,
							 m_ScreenWidth,
							 m_ScreenHeight,
							 NULL,
							 NULL,
							 hInstance,
							 NULL);

	if(!wndHandle)
		return false;
	
	ShowWindow(wndHandle, nShowCmd);
	UpdateWindow(wndHandle);
	return true;
}

/**************************************
* khởi tạo Direct3D
***************************************/
bool CInit::initDirect3D()
{
	_pd3d = Direct3DCreate9(D3D_SDK_VERSION);
	if(NULL == _pd3d)
	{
		return false;
	}

	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = !m_IsFullScreen;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferHeight = m_ScreenHeight;
	d3dpp.BackBufferWidth = m_ScreenWidth;
	d3dpp.hDeviceWindow = wndHandle;

	if(FAILED(_pd3d->CreateDevice(D3DADAPTER_DEFAULT,
								 D3DDEVTYPE_HAL,
								 wndHandle,
								 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
								 &d3dpp,
								 &_pd3dDevice)))
	{
		return false;
	}
	return true;
}

bool CInit::initSound()
{
	return initDirectSound(wndHandle);
}

/**************************************
* hàm kết xuất hình ảnh render
***************************************/
void CInit::render()
{
	if(NULL == _pd3dDevice)
		return;
	if (_pd3dDevice->BeginScene()) 
	{
		_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(123,231,255), 1.0f, 0);
		_pd3dDevice->EndScene();
	}
	_pd3dDevice->Present(NULL,NULL,NULL,NULL);
}
